<template>
  <div class="modal-header">
    <div class="modal-title">{{ l('SelectUser') }}</div>
  </div>
  <slot></slot>
  <div class="modal-body">
    <g-dynamic-table
      type="common-lookup"
      ref="table"
      :fetch="fetchDataList"
      @action-click="handleActionClick"
    >
    </g-dynamic-table>
  </div>
  <div class="modal-footer">
    <a-button :disabled="saving" @click="close()" type="button">
      {{ l('Cancel') }}
    </a-button>
  </div>
</template>

<script lang="ts">
  import { ModalPagedListingComponentBase } from '/@/shared/component-base';
  import { CommonLookupFindUsersInput, CommonLookupServiceProxy } from '/@/shared/service-proxies';
  import { defineComponent } from 'vue';
  import GDynamicTable from '/@/shared/components/g-dynamic-table/index.vue';

  export default defineComponent({
    name: 'CommonLookup',
    components: {
      GDynamicTable,
    },
    mixins: [ModalPagedListingComponentBase],
    props: {
      pageDataList: Number,
    },
    data() {
      return {
        // 输入框
        filterText: '',
        commonLookupServiceProxy: new CommonLookupServiceProxy(),
        com: new CommonLookupFindUsersInput(),
      };
    },
    methods: {
      /**
       * 获取数据
       */
      fetchDataList() {
        if (!!this.$refs.table) {
          const { maxResultCount, skipCount, filterText } = (
            this.$refs.table as any
          ).getSearchProps();
          this.com.tenantId = this.pageDataList;
          this.com.maxResultCount = maxResultCount;
          this.com.skipCount = skipCount;
          this.com.filterText = filterText;
          return new Promise((resolve, reject) => {
            this.commonLookupServiceProxy
              .findUsers(this.com)
              .finally(() => {})
              .then((res) => {
                resolve(res);
              })
              .catch((error) => {
                reject(error);
              });
          });
        }
      },
      /**
       * 按钮回调事件
       */
      handleActionClick(event: string, item?: any) {
        console.log(event);
        switch (event) {
          case 'Select':
            this.selectItem(item);
            break;
          default:
            break;
        }
      },
      /**
       * 选择
       */
      selectItem(item) {
        this.success(item);
      },
    },
  });
</script>
